﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using qltscd.Helper;
using System.Web.Routing;

namespace qltscd.Controllers
{
    public class HangSanXuatController : BaseController
    {
        protected override void OnActionExecuting (ActionExecutingContext filterContext) {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenDanhMuc] == '0') {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }

        #region Properties Section
        private QLTBEntities db = new QLTBEntities();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            HSXScreenClient screen = new HSXScreenClient();
            if (Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT] != null)
            {
                screen = (HSXScreenClient)Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT];
            }
            Search(screen);
            return View(screen);
        }

        /*private void Search(DVQLScreenClient screen)
        {
            throw new NotImplementedException();
        }*/
        [HttpPost]
        public ActionResult Index(HSXScreenClient screen)
        {
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index()
        {
            HSXScreenClient screen = new HSXScreenClient();
            if (Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT] != null)
            {
                screen = (HSXScreenClient)Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSHangSanXuat));
        }
        public ActionResult Create()
        {
            return View();
        }
        #endregion

        #region CRUD Section
        [HttpPost]
        public ActionResult Create(DanhMucHangSanXuat danhmuchangsanxuat)
        {
            if (ModelState.IsValid)
            {
                db.DanhMucHangSanXuats.AddObject(danhmuchangsanxuat);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(danhmuchangsanxuat);
        }
        #endregion

        #region Helper Section
        private void Search(HSXScreenClient screen)
        {
            if (Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT] != null)
            {
                screen = (HSXScreenClient)Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT];
            }
            else
            {
                screen = new HSXScreenClient();
            }
            screen.DSHangSanXuat.Clear();
            var list = db.DanhMucHangSanXuats.OrderBy(i => i.TenHangSanXuat).ToList();
            foreach (DanhMucHangSanXuat hsx in list)
            {
                qltscd.Models.BaseClient.HangSanXuat hsxClient = new Models.BaseClient.HangSanXuat();
                ObjectHelper.Copy(hsx, hsxClient);
                screen.DSHangSanXuat.Add(hsxClient);
            }
            Session[SessionScreenContansts.HANG_SAN_XUAT_SCREENCLIENT] = screen;
        }
        #endregion

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
    }
}